// pages/order/index.js
const app = getApp() // 引入app
const _request = require("../../utils/request.js"); // 引入require;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl, //全局img路径
    isShow_01: false,
    listData_01: [['全部','待接单', '待核销', '已完成', '已退款']],
    picker_01_data: ['全部'],
    weekDays: [],
    orders:[],
    status:0,
    getday: ''
  },
  
  showPicker_01: function () {
    this.setData({
      isShow_01: true
    })
  },
  sureCallBack_01(e) {
    let data = e.detail
    this.setData({
      isShow_01: false,
      picker_01_data: e.detail.choosedData,
      picker_01_index: JSON.stringify(e.detail.choosedIndexArr),
      status: e.detail.choosedIndexArr[0]
    })
    this.query(this.data.getday,this.data.status);
  },
  cancleCallBack_01() {
    this.setData({
      isShow_01: false,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var weekDays = [];
    for (let i = 0; i < 7; i++) {
      var now = this.addDate(i);
      var date = now.getDate();
      var day = this.getWeek(now.getDay());
      var item = { date: date, day: day, checked: i == 0 ? true : false, datetime: this.getCompletDate(now)};
      weekDays.push(item);
    }
    this.setData({
      weekDays: weekDays,
      getday: weekDays[0]['datetime']
    })
    this.query(this.data.getday,this.data.status);
  },
  addDate: function (days) {
    var date = new Date();
    date.setDate(date.getDate() + days);
    return date;
  },
  getWeek: function (day) {
    var day1 = "";
    switch (day) {
      case 0: day1 = "日";
        break;
      case 1: day1 = "一";
        break;
      case 2: day1 = "二";
        break;
      case 3: day1 = "三";
        break;
      case 4: day1 = "四";
        break;
      case 5: day1 = "五";
        break;
      case 6: day1 = "六";
        break;
    }
    return day1;
  },
  getCompletDate: function (date){
    var month = date.getMonth() + 1;
    var day = date.getDate();
    return date.getFullYear() + this.getFormatDate(month) + this.getFormatDate(day);
  },
  getFormatDate :function (arg) {
    if (arg == undefined || arg == '') {
      return '';
    }
    var re = arg + '';
    if (re.length < 2) {
      re = '0' + re;
    }
    return re;
  },
  onWorkTap: function (e) {
    let index = e.currentTarget.dataset.item;
    var weekDaysTemp = [];
    weekDaysTemp = this.data.weekDays;
    for (let i = 0; i < weekDaysTemp.length; i++) {
      weekDaysTemp[i]['checked'] = false;
    }
    weekDaysTemp[index]['checked'] = weekDaysTemp[index]['checked'] ? false : true;
    this.setData({
      weekDays: weekDaysTemp,
      getday: weekDaysTemp[index]['datetime']
    });
    this.query(this.data.getday,this.data.status);
  },
  query: function (datetime,status){
    let storeId = wx.getStorageSync('storeId');
    _request.postObj('/storeManage/timemanage', {
      datetime: datetime,
      storeId: storeId,
      status
    }).then((res) => {
      this.setData({
        orders: res.data.data
      });
    }).catch((error) => {
      console.log(error, 'error');
    })
  }
})